/**
 * ValidateUtil.java
 * Version 1.0.0
 * Created on 2018年11月12日
 *
 */
package com.yincheng.oes.business.base.importexport;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.yincheng.oes.business.base.entity.Question;
import com.yincheng.oes.business.base.entity.QuestionKnowlegeExt;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年11月12日
 * <p>
 * 功能描述：
 * <p>
 * 邮箱地址：446948434@qq.com
 */
public class ValidateUtil {

    public static QuestionResponse checkExercises(List<Question> list) {
        QuestionResponse response = new QuestionResponse();
        for (int i = 0; i < list.size(); i++) {
            Question row = list.get(i);
            String quesCode = StringUtils.trim(row.getQuesCode());
            String quesType = StringUtils.trim(row.getQuesType());
            String stem = row.getStem();
            String question = row.getQuestion();
            String relationCode = row.getRelationCode();
            String optionA = row.getOptionA();
            String optionB = row.getOptionB();
            String optionC = row.getOptionC();
            String optionD = row.getOptionD();
            String answer = StringUtils.trim(row.getAnswer());

            if (StringUtils.isEmpty(quesCode)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，题目编号不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(quesType)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，题目类型不能为空。";
                response.setMessage(message);
                return response;
            }
            switch (quesType) {
                case "A1":
                    quesType = "1";
                    row.setQuesType(quesType);
                    if (!StringUtils.isEmpty(stem)) {
                        response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                        String message = "第" + (i + 2) + "行，主题干必须为空。";
                        response.setMessage(message);
                        return response;
                    }
                    if (!StringUtils.isEmpty(relationCode)) {
                        response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                        String message = "第" + (i + 2) + "行，关联关系值必须为空。";
                        response.setMessage(message);
                        return response;
                    }
                    break;
                case "A2":
                    quesType = "2";
                    row.setQuesType(quesType);
                    if (StringUtils.isEmpty(stem)) {
                        response.setCode(QuestionResponse.DATA_NULL_ERROR);
                        String message = "第" + (i + 2) + "行，主题干不能为空。";
                        response.setMessage(message);
                        return response;
                    }
                    if (StringUtils.isEmpty(relationCode)) {
                        response.setCode(QuestionResponse.DATA_NULL_ERROR);
                        String message = "第" + (i + 2) + "行，关联关系值不能为空。";
                        response.setMessage(message);
                        return response;
                    }
                    break;
                case "B":
                    quesType = "3";
                    row.setQuesType(quesType);
                    if (!StringUtils.isEmpty(stem)) {
                        response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                        String message = "第" + (i + 2) + "行，主题干必须为空。";
                        response.setMessage(message);
                        return response;
                    }
                    break;
                case "X":
                    quesType = "4";
                    row.setQuesType(quesType);
                    if (!StringUtils.isEmpty(stem)) {
                        response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                        String message = "第" + (i + 2) + "行，主题干必须为空。";
                        response.setMessage(message);
                        return response;
                    }
                    if (!StringUtils.isEmpty(relationCode)) {
                        response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                        String message = "第" + (i + 2) + "行，关联关系值必须为空。";
                        response.setMessage(message);
                        return response;
                    }
                    break;
                default:
                    response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                    String message = "第" + (i + 2) + "行，题目类型内容不正确。";
                    response.setMessage(message);
                    return response;
            }
            if (StringUtils.isEmpty(question)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，题目信息不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(optionA)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，选项A不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(optionB)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，选项B不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(optionC)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，选项C不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(optionD)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，选项D不能为空。";
                response.setMessage(message);
                return response;
            }
            if (StringUtils.isEmpty(answer)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，正确答案不能为空。";
                response.setMessage(message);
                return response;
            }
        }
        response.setCode(QuestionResponse.SUCCESS);
        String message = "数据校验正常";
        response.setMessage(message);
        return response;
    }

    /**
     * 校验大纲并转换成大纲关系集合
     *
     * @param outlines
     * @param outlineExts
     * @return
     */
    public static QuestionResponse checkOutline(List<QuestionKnowlegeExt> outlines,
            List<QuestionKnowlegeExt> outlineExts) {
        QuestionResponse response = new QuestionResponse();
        for (int i = 0; i < outlines.size(); i++) {
            QuestionKnowlegeExt row = outlines.get(i);
            String quesCode = StringUtils.trim(row.getQuesCode());
            String outlineCode = row.getOutlineCode();
            String outlineType = StringUtils.trim(row.getOutlineType());
            Integer yearNum = row.getYearNum();
            if (StringUtils.isEmpty(outlineCode))
                continue;
            if (StringUtils.isEmpty(outlineType)) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，考试类别不能为空。";
                response.setMessage(message);
                return response;
            }
            if (yearNum == null) {
                response.setCode(QuestionResponse.DATA_NULL_ERROR);
                String message = "第" + (i + 2) + "行，年代不能为空。";
                response.setMessage(message);
                return response;
            }
            switch (outlineType) {
                case "西医综合":
                    outlineType = "1";
                    break;
                case "执业医师":
                    outlineType = "2";
                    break;
                case "执业历年真题":
                    outlineType = "3";
                    break;
                default:
                    response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
                    String message = "第" + (i + 2) + "行，考试类别只支持西医综合和执业医师。";
                    response.setMessage(message);
                    return response;
            }
            String[] split = StringUtils.split(outlineCode, ",");
            for (String string : split) {
                QuestionKnowlegeExt ext = new QuestionKnowlegeExt();
                ext.setQuesCode(quesCode);
                ext.setOutlineCode(string.trim());
                ext.setOutlineType(outlineType);
                ext.setYearNum(yearNum);
                outlineExts.add(ext);
            }

        }
        response.setCode(QuestionResponse.SUCCESS);
        String message = "数据校验正常";
        response.setMessage(message);
        return response;
    }
}
